layui.use(['form','laypage', 'layer'], function(){
  var laypage = layui.laypage
  ,layer = layui.layer
  ,form = layui.form;
  
  
  
  initPage();
  $("#search-btn").on('click',function(){initPage();})
  $("#add-btn").on('click',function(){x_admin_show('新增','../dataset/toAdd',null,null,true)});
  
  function initPage(){
	  var data = getData();
	  ajaxPost("../dataset/list",data,
			  function(data){
		  createData(data.data.list);
		  laypage.render({
			  elem:'page',
			  count:data.data.total,
			  layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
		    ,jump: function(obj,first){
		    	if(!first){
		    		toPage(obj.curr,obj.limit);
		    	}
		    }
		  });
	  },
			  function(data){
				  layer.alert(data.errorMsg,{
						icon:5,
						title:"错误"
					});
			  });
  }
  
 window.initPage = initPage;
 function toPage(pageIndex,pageSize){
	  
	var data = getData(pageIndex,pageSize);
	  ajaxPost("../dataset/list",data,
			  function(data){createData(data.data.list);},
			  function(data){
				  layer.alert(data.errorMsg,{
						icon:5,
						title:"错误"
					});
			  });
  }
  
  
  function getData(pageIndex,pageSize){
 	  var data = {};
 	  var datasetName = $("#datasetName").val();
 	  data["datasetName"] = datasetName;
 	  data["pageIndex"] = pageIndex?pageIndex:1;
 	  data["pageSize"] = pageSize?pageSize:10;
 	  return data;
   	}
  
  function createData(data){
	  
	  var content = [];
	  for(var i =0 ;i < data.length;i++){
		  var dataset = data[i];
		  content.push('<tr>');
		  content.push('<td>'+dataset.datasetName+'</td>');
		  content.push('<td>'+(dataset.datasetStatus == 1?"有效":"删除")+'</td>');
		  content.push(' <td class="td-manage"> '+
	                '<a title="编辑"  onclick="x_admin_show(\'编辑\',\'../dataset/toEdit?datasetId='+dataset.id+'\',null,null,true)" href="javascript:;">'+
	                  '<i class="layui-icon">&#xe642;</i>'+
	                '</a>'+
	            '</td>');
		  content.push('</tr>');
	  }
	  
	  $("#dg").empty().append(content.join(""));
	  form.render('checkbox');
  }
  
  
  
  function delApp(id){
	  layer.confirm("是否确认删除",
				{btn:['是','否']},
				function(index){
					ajaxPost('../app/deleteApp',{ids:id},
							  function(data){
							   layer.alert("删除成功", {
								icon: 1,
								title: "成功"
								});
							   initPage();
					          },
					          function(data){
					        	  layer.alert(data.errorMsg,{
					        		  icon: 5,
					  				  title: "错误"
					        	  });
					          }
					          );
				
							layer.close(index);},
				function(index){layer.close(index);});
   }
  
  
  window.delApp = delApp;
  
  
  function deleteAll(){
	  var data = [];
	  $("input[name='checkbox']:checked").each(function(){
		  data.push($(this).val());
	  });
	  if(typeof data=="undefined" ||data.length == 0){
		  layer.alert("请选择需要删除的数据",{
    		  icon: 5,
				  title: "错误"
    	  });
		  return false;
	  }
	  layer.confirm("是否确认删除",
				{btn:['是','否']},
				function(index){
					 ajaxPost('../app/deleteApp',{ids:data.join(",")},
							    function(data){
									  layer.alert("删除成功", {
											icon: 1,
											title: "成功"
											});
										   initPage();
								  },
						          function(data){
						        	  layer.alert(data.errorMsg,{
						        		  icon: 5,
						  				  title: "错误"
						        	  });
						          });
					layer.close(index);},
				function(index){layer.close(index);});
  }
});
